package com.lg.cms.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.cms.entity.*;

import com.lg.cms.mapper.*;
import com.lg.cms.service.CommentService;
import com.lg.cms.vo.LayerTableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @Description:评论管理控制器
 * @create: 2020-09-22 10:47
 */


@Controller
@Slf4j
@RequestMapping("/admin/comment")
public class CommentManageController {

    @Autowired
    CommentService commentService;
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 评论页面
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/comment")
    public String comment(Model model, HttpServletRequest request){
        model.addAttribute("request", request);
        return "/admin/interaction_management/commentCategory";
    }

    /**
     * 根据 审核状态 查找的所有评论
     * @param page
     * @param limit
     * @param showComment
     * @return
     */
    @ResponseBody
    @GetMapping("/showComment")
    public LayerTableVO<Comment> shouComment(Integer page,Integer limit, Boolean showComment){
        Page<Comment> page1=new Page<>(page,limit);
        LayerTableVO<Comment> commentLayerTableVO=new LayerTableVO<>();

        commentLayerTableVO= commentService.selectCommentByCheck(page1,showComment);



        return commentLayerTableVO;
    }

    /**
     * 根据 审核状态 查找文章的所有评论
     * @return
     */
    @ResponseBody
    @GetMapping("/article/{id}")
    public Article commentCheck( @PathVariable("id")Integer authorId){

        return commentService.selectAuthorIdCheck(authorId);
    }

    /**
     * 设置文章的 审核状态
     * @param id
     * @param showComment
     * @return
     */
    @ResponseBody
    @GetMapping("/commentCheck/check")
    public R updateCheck(@RequestParam("id")Integer id,Boolean showComment){

        int n =commentService.updateCheck(id,showComment);

        R r=new R();

        if (n>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(203);
            r.setMsg("修改失败");
        }
        return r;
    }

    /**
     * 批量设置文章的 审核状态
     * @param ids
     * @param showComment
     * @return
     */
    @ResponseBody
    @GetMapping("/commentCheck/checks")
    public R updateChecks(@RequestParam("Ids")String ids,Boolean showComment){
        boolean tf=commentService.updateChecks(ids,showComment);
        R r=new R();

        if (tf){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(203);
            r.setMsg("修改失败");
        }
        return r;
    }



    /**
     * 删除文章里的评论
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/commentCheck/del")
    public R delCheck(@RequestParam("Id")Integer id){
        int n=commentService.deletCheckById(id);
        R r=new R();

        if (n>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(203);
            r.setMsg("删除失败");
        }
        return r;
    }

    /**
     * 批量删除文章里的评论
     * @param ids
     * @return
     */
    @ResponseBody
    @GetMapping("/commentCheck/dels")
    public R delChecks(@RequestParam("ids")String ids){
        int n=commentService.deletCheckByIds(ids);
        R r=new R();

        if (n>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(203);
            r.setMsg("删除失败");
        }
        return r;
    }

}
